﻿<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">

    <xsl:template match="section">
        <xsl:choose>
            <xsl:when test="$rootid = @id">
                <xsl:call-template name="section.page.sequence"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="id">
                    <xsl:call-template name="object.id"/>
                </xsl:variable>
                <xsl:variable name="renderas">
                    <xsl:choose>
                        <xsl:when test="@renderas = 'sect1'">1</xsl:when>
                        <xsl:when test="@renderas = 'sect2'">2</xsl:when>
                        <xsl:when test="@renderas = 'sect3'">3</xsl:when>
                        <xsl:when test="@renderas = 'sect4'">4</xsl:when>
                        <xsl:when test="@renderas = 'sect5'">5</xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="''"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:variable name="level">
                    <xsl:choose>
                        <xsl:when test="$renderas != ''">
                            <xsl:value-of select="$renderas"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="section.level"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <!-- xsl:use-attribute-sets takes only a Qname, not a variable -->
                <xsl:choose>
                    <xsl:when test="$level = 1">
                        <fo:block id="{$id}" xsl:use-attribute-sets="section.level1.properties">
                            <xsl:if test="
                                (parent::chapter or parent::appendix or parent::preface)
                                and
                                (generate-id() = generate-id(parent::node()/section[1]))">
                                <!-- FBA: Nouvelle page pour la première section d'un composant chapter-like. -->
                                <!-- FIXME: placer dans section.level1.properties ou utiliser un titlepage-separator intelligent -->
                                <xsl:attribute name="break-before">page</xsl:attribute>
                            </xsl:if>
                            <xsl:call-template name="section.content"/>
                        </fo:block>
                    </xsl:when>
                    <xsl:when test="$level = 2">
                        <fo:block id="{$id}" xsl:use-attribute-sets="section.level2.properties">
                            <xsl:call-template name="section.content"/>
                        </fo:block>
                    </xsl:when>
                    <xsl:when test="$level = 3">
                        <fo:block id="{$id}" xsl:use-attribute-sets="section.level3.properties">
                            <xsl:call-template name="section.content"/>
                        </fo:block>
                    </xsl:when>
                    <xsl:when test="$level = 4">
                        <fo:block id="{$id}" xsl:use-attribute-sets="section.level4.properties">
                            <xsl:call-template name="section.content"/>
                        </fo:block>
                    </xsl:when>
                    <xsl:when test="$level = 5">
                        <fo:block id="{$id}" xsl:use-attribute-sets="section.level5.properties">
                            <xsl:call-template name="section.content"/>
                        </fo:block>
                    </xsl:when>
                    <xsl:otherwise>
                        <fo:block id="{$id}" xsl:use-attribute-sets="section.level6.properties">
                            <xsl:call-template name="section.content"/>
                        </fo:block>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>
